package cn.snowsoft.iot.module.warning.controller.admin.warning.action;

import cn.snowsoft.iot.framework.common.pojo.CommonResult;
import cn.snowsoft.iot.module.warning.controller.admin.warning.action.vo.WarningActionPageVO;
import cn.snowsoft.iot.module.warning.controller.admin.warning.action.vo.WarningActionSaveOrUpdateVO;
import cn.snowsoft.iot.module.warning.dal.dataobject.warning.action.MQTTDO;
import cn.snowsoft.iot.module.warning.dal.dataobject.warning.action.WarningActionDO;
import cn.snowsoft.iot.module.warning.mqtt.MqttClientUtil;
import cn.snowsoft.iot.module.warning.service.warning.action.IWarningActionService;
import cn.snowsoft.iot.module.warning.utils.StringToList;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Tag(name = "管理后台 - 告警动作")
@RestController
@RequestMapping("/warning/action")
public class WarningActionController {

    @Resource
    private IWarningActionService actionService;


    @PostMapping("submit")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "新增或修改")
    public CommonResult submit(@RequestBody WarningActionSaveOrUpdateVO action) {
        return actionService.saveUpdate(action);
    }

    @GetMapping("/page")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "分页")
    public CommonResult<IPage<WarningActionDO>> page(WarningActionPageVO actionPage) {
        IPage<WarningActionDO> page = actionService.selectWarningPage(actionPage);
        return CommonResult.success(page);
    }

    @GetMapping("/detail")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "详情")
    public CommonResult detail (Long id) {
        return actionService.getActionById(id);
    }

    @GetMapping("/remove")
    @ApiOperationSupport(order = 4)
    public CommonResult<Boolean> remove(@RequestParam String ids) {
        return CommonResult.success(actionService.removeAction(StringToList.toLongList(ids)));
    }

    @PostMapping("/testMQTTConnection")
    @ApiOperationSupport(order = 5)
    public CommonResult testConnection(@RequestBody MQTTDO mqttdo) {

        MqttClientUtil temp = new MqttClientUtil(
                StringUtils.isBlank(mqttdo.getClsId()) ? UUID.randomUUID().toString().substring(18) : mqttdo.getClsId(),
                mqttdo.getUserName(),
                mqttdo.getPassWord(),
                mqttdo.getMqttAddress());
        if (temp.connect()) {
            temp.closeConnect(1000);
            return CommonResult.success("连接成功");
        } else {
            return CommonResult.error(400, "连接参数错误，请检查");
        }
    }


    @PostMapping("/getByIds")
    public CommonResult<List<WarningActionDO>> getByIds(@RequestParam("ids") String ids) {
        return CommonResult.success(actionService.getByIds(ids));
    }
}
